package com.voc.im;

import com.alibaba.fastjson.JSON;
import com.voc.im.controller.account.Account2Controller;
import com.voc.im.manager.StageManager;
import io.datafx.controller.flow.Flow;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import okhttp3.*;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main extends Application {

    // 定义偏移量，用于处理窗口移动
    private double xOffset = 0;
    private double yOffset = 0;

    @Override
    public void start(Stage primaryStage) throws Exception {

//        Parent root = FXMLLoader.load(getClass().getResource("/fxml/.fxml"));
//        primaryStage.initStyle(StageStyle.TRANSPARENT);
//        primaryStage.setScene(new Scene(root));
//        primaryStage.show();


//        StackPane root = new Flow(Webview1.class).start();
//        Scene scene = new Scene(root);
//        primaryStage.setScene(scene);
//        primaryStage.show();

//        new Flow(MainController.class).startInStage(primaryStage);

//        StackPane root = new Flow(Account2Controller.class).start();
//        Scene scene = new Scene(root, Color.TRANSPARENT);
//        primaryStage.initStyle(StageStyle.TRANSPARENT);
//        primaryStage.setScene(scene);
//        primaryStage.show();

//        root.setOnMousePressed(event -> {
//            xOffset = event.getSceneX();
//            yOffset = event.getSceneY();
//        });
//
//        root.setOnMouseDragged(event -> {
//            primaryStage.setX(event.getScreenX() - xOffset);
//            primaryStage.setY(event.getScreenY() - yOffset);
//        });

        StageManager.stage(Account2Controller.class);
    }


    public static void main(String[] args) {
//        BasicDataSource basicDataSource = new BasicDataSource();
//        basicDataSource.setDriverClassName("org.sqlite.JDBC");
//        basicDataSource.setUrl("jdbc:sqlite:linlimin1.db");
//        QuickDAO.newInstance().dataSource(basicDataSource)
//                .packageName("com.voc.im.entity")
//                .build();
        launch(args);

//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                sendRandomCodeAction("15260925089");
//            }
//        }).start();

    }

    public static void sendRandomCodeAction(String phone) {
        Test10086 test10086 = loadToken(phone);
        if (test10086 == null) {
            System.out.println("test10086null");
            return;
        }
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded; charset=UTF-8");
        RequestBody body = RequestBody.create(mediaType, "userName=" + phone + "&type=01&channelID=12014");
        Request request = new Request.Builder()
                .url("https://login.10086.cn/sendRandomCodeAction.action")
                .method("POST", body)
                .addHeader("Accept", "application/json, text/javascript, */*; q=0.01")
                .addHeader("Accept-Encoding", "gzip, deflate, br")
                .addHeader("Accept-Language", "zh-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7")
                .addHeader("Connection", "keep-alive")
                .addHeader("Content-Length", "44")
                .addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                .addHeader("Host", "login.10086.cn")
                .addHeader("Origin", "https://login.10086.cn")
                .addHeader("Referer", "https://login.10086.cn/html/login/touch.html")
                .addHeader("Sec-Fetch-Dest", "empty")
                .addHeader("Sec-Fetch-Mode", "cors")
                .addHeader("Sec-Fetch-Site", "same-origin")
                .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36")
                .addHeader("X-Requested-With", "XMLHttpRequest")
                .addHeader("Xa-before", test10086.result)
//                .addHeader("Cookie", "sendflag=20200506114923785876")
                .build();
        try {
            Response response = client.newCall(request).execute();
            System.out.println(response.code() + ",," + response.body().string());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static Test10086 loadToken(String phone) {

        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded; charset=UTF-8");
        RequestBody body = RequestBody.create(mediaType, "userName=" + phone);
        Request request = new Request.Builder()
                .url("https://login.10086.cn/loadToken.action")
                .method("POST", body)
                .addHeader("Accept", "application/json, text/javascript, */*; q=0.01")
                .addHeader("Accept-Encoding", "gzip, deflate, br")
                .addHeader("Accept-Language", "zh-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7")
                .addHeader("Connection", "keep-alive")
                .addHeader("Content-Length", "20")
                .addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                .addHeader("Host", "login.10086.cn")
                .addHeader("Origin", "https://login.10086.cn")
                .addHeader("Sec-Fetch-Dest", "empty")
                .addHeader("Sec-Fetch-Mode", "cors")
                .addHeader("Sec-Fetch-Site", "same-origin")
                .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36")
                .addHeader("X-Requested-With", "XMLHttpRequest")
//                .addHeader("Cookie", "sendflag=20200506114923785876")
                .build();
        try {
            Response response = client.newCall(request).execute();
            if (response.code() != 200) {
                System.out.println(response.code());
                return null;
            }
            Test10086 test10086 = JSON.parseObject(response.body().string(), Test10086.class);
            System.out.println(test10086.getResult());
            return test10086;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    static class Test10086 {

        String result;
        String code;
        String desc;

        public String getResult() {
            return result;
        }

        public void setResult(String result) {
            this.result = result;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getDesc() {
            return desc;
        }

        public void setDesc(String desc) {
            this.desc = desc;
        }
    }
}
